<template>
  <div class="list_container flex both-100">
    <Breadcrumb :title="pageTitle" />
    <div class="content_container">
      <!-- 搜索模块 -->
      <el-form ref="searchMap" :inline="true" :model="searchMap" class="serach_wrapper flex-space-between">
        <div class="search">
          <el-form-item prop="configName">
            <el-input v-model="searchMap.configName" placeholder="请输入参数名称" size="small" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item prop="configKey">
            <el-input v-model="searchMap.configKey" placeholder="请输入参数键名" size="small" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item prop="configType">
            <el-select v-model="searchMap.configType" placeholder="系统内置" size="small" clearable>
              <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item width="80px">
            <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" size="small"></el-date-picker>
          </el-form-item>

          <el-form-item class="flex">
            <el-button @click="handleSearch()" class="btn_primary">查询</el-button>
            <el-button @click="handleReset()" class="btn_cancel">重置</el-button>
          </el-form-item>
        </div>
        <div class="edit">
          <el-form-item class="flex">
            <el-button @click="handleAdd" class="btn_primary" icon="el-icon-plus"> 新增 </el-button>
            <el-button @click="handleEdit" class="btn_primary" icon="el-icon-edit"> 修改 </el-button>
            <el-button @click="handleDelete" class="btn_primary" icon="el-icon-delete"> 删除 </el-button>
            <el-button @click="handleExport" class="btn_primary" icon="el-icon-download"> 导出 </el-button>
            <el-button @click="handleRefreshCache" class="btn_primary" icon="el-icon-refresh-right"> 刷新缓存 </el-button>
          </el-form-item>
        </div>
      </el-form>
      <!-- 搜索模块over -->

      <!-- 表格模块 -->
      <div class="table_container">
        <el-table :data="dataList" v-loading="loading" stripe class="per_table" @selection-change="handleSelectionChange" style="width: 100%">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="参数主键" align="center" prop="configId" />
          <el-table-column label="参数名称" align="center" prop="configName" :show-overflow-tooltip="true" />
          <el-table-column label="参数键名" align="center" prop="configKey" :show-overflow-tooltip="true" />
          <el-table-column label="参数键值" align="center" prop="configValue" :show-overflow-tooltip="true" />
          <el-table-column label="系统内置" align="center" prop="configType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.configType" />
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark" width="200" :show-overflow-tooltip="true" />
          <el-table-column label="创建时间" align="center" prop="createTime" :show-overflow-tooltip="true" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150" fixed="right">
            <template slot-scope="scope">
              <div class="button-group">
                <span type="text" class="tag_primary" size="small" @click="handleEdit(scope.row)">修改</span>
                <span type="text" class="tag_warning" size="small" @click="handleDelete(scope.row)">删除</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 表格模块over -->

      <!-- 分页 -->
      <Pagination v-show="total > 0" :total="total" :page.sync="searchMap.pageNum" :limit.sync="searchMap.pageSize" @change="getList"></Pagination>
      <!-- 分页over -->

      <!-- 弹窗 新增/编辑 -->
      <ConfigForm :formData="formData" :dictMap="dict" :titleIndex="titleIndex" :dialogVisible="dialogVisible" @cancel="cancel" @getList="getList"></ConfigForm>
      <!-- 弹窗 新增/编辑 over -->
    </div>
  </div>
</template>

<script>
import { listConfig, getConfig, delConfig, refreshCache } from "@/api/system/config";
import Breadcrumb from '@/components/Breadcrumb';
import CustomForm from '@/components/CustomForm';
import ConfigForm from '@/views/system/config/ConfigForm';

export default {
  name: 'role',
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 日期范围
      dateRange: [],
      // 搜索条件
      searchMap: {
        pageNum: 1,
        pageSize: 10,
        configName: undefined,
        configKey: undefined,
        configType: undefined,
      },
      // 提交表单数据
      formData: {
        configId: '',
        configName: '',
        configKey: '',
        configValue: '',
        configType: 'Y',
        remark: '',
      },
      // 数据列表
      dataList: [],
      // 总条数
      total: 0,
      // 弹窗-新增，编辑头文字
      titleIndex: 0,
      // 弹窗-新增/编辑-显示隐藏
      dialogVisible: false,
    };
  },
  created() {
    this.getList();
  },
  components: {
    Breadcrumb,
    CustomForm,
    ConfigForm
  },
  computed: {
    pageTitle() {
      return '参数设置';
    },
  },
  methods: {
    getList() {
      this.loading = true;
      listConfig(this.addDateRange(this.searchMap, this.dateRange)).then(res => {
        this.dataList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    /** 查询按钮操作 */
    handleSearch() {
      this.searchMap.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    handleReset() {
      this.dateRange = [];
      this.resetForm('searchMap');
      this.searchMap.pageNum = 1;
      this.getList();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.configId);
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.dialogVisible = true;
      this.titleIndex = "添加参数";
    },
    // 修改弹窗（仅展示）
    handleEdit(row) {
      this.reset();
      const configId = row.configId || this.ids;
      getConfig(configId).then(res => {
        this.formData = res.data;
        this.dialogVisible = true;
        this.titleIndex = "修改参数";
      });
    },
    // 删除数据
    handleDelete(row) {
      const ids = row.configId || this.ids;
      if (this.$utils.isNotEmpty(ids)) {
        this.$confirm('是否确认删除所选数据项？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          delConfig(ids).then(res => {
            if (res.code == 200) {
              this.$message.success('删除成功');
              this.getList();
            }
          });
        });
      } else {
        this.$message.warning('请选择数据进行删除');
      }
    },
    // 导出数据
    handleExport() {
      this.download(
        'system/config/export',
        {
          ...this.searchMap,
        },
        `参数日志${new Date().getTime()}.xlsx`
      );
    },
    // 表单重置
    reset() {
      this.formData = {
        configId: undefined,
        configName: undefined,
        configKey: undefined,
        configValue: undefined,
        configType: "Y",
        remark: undefined
      };
    },
    // 弹窗新增编辑-取消
    cancel() {
      this.dialogVisible = false;
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$message.success("刷新成功");
      });
    }
  },
};
</script>

<style lang="less" scoped>
.list_container {
  flex-direction: column;
  background: url('@/assets/images/home/content.png') no-repeat;
  background-size: cover;

  .content_container {
    padding: 15px 20px;
    overflow-y: scroll;
  }

  .table_container {
    margin-bottom: 25px;
  }

  .pagination_container {
    display: flex;
    justify-content: space-between;
    height: 32px;
  }
}</style>
